package Year22_Month06.day17;

import java.util.ArrayList;
import java.util.List;

public class 路径总和_递归写法 {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if(root == null) return false;
        List<Integer> list = new ArrayList<>();//用一个顺序表来存储每个路径的和
        solve(root,0,list);
        for(Integer a:list) {
            if(a == targetSum){
                return true;
            }
        }
        return false;
    }


    public void solve(TreeNode node,int sum,List<Integer> list){
        if(node == null) return;
        sum += node.val;//不为空就存储当前的值
        if(node.left == null && node.right == null){
            list.add(sum);
        } else{
            solve(node.left,sum,list);
            solve(node.right,sum,list);
        }
    }
}
